package com.dqyt.khjc.service;

import com.alibaba.fastjson.JSON;
import com.dqyt.common.check.CheckTool;
import com.dqyt.common.constant.AuthorityID;
import com.dqyt.common.utils.UserInfoUtil;
import com.dqyt.khjc.bean.BSms;
import com.dqyt.khjc.bean.ResultMsg;
import com.dqyt.khjc.bean.User;
import com.dqyt.khjc.mapper.BSmsMapper;
import com.dqyt.khjc.sms.SMSConfig;
import com.dqyt.khjc.sms.SMSParm;
import com.dqyt.khjc.sms.SMSTool;
import com.dqyt.khjc.utils.PhoneUtil;
import com.dqyt.khjc.utils.TimeUtil;
import com.dqyt.khjc.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * 资质
 * 
 * @author Administrator
 *
 */
@Service
public class SmsService {

	@Autowired
	private BSmsMapper mSmsMapper;

	/**
	 * 每次发短信的隔离时间
	 */
	public final static long V_TIME = 60 * 1000L;

	/**
	 * 获取验证码
	 * @return
	 */
	public ResultMsg getSmsCode(HttpServletRequest request,String phone){
		ResultMsg ret = new ResultMsg(); // 声明一个返回值
		try {
			// 检查用户是否登录Token是否正确
			ret = UserInfoUtil.isAuthori_Web(request);
			// 如果有错误信息
			if (ret != null) {
				return ret;
			}
			ret = new ResultMsg(); // 声明一个返回值
			boolean istr = PhoneUtil.isMobileNO(phone);
			if (!istr) {
				ret.setMsg("手机号格式不正确");
				ret.setCode(ResultMsg.ERROR);
				return ret;
			}
			User user = UserInfoUtil.getUser_Web(request);

			// 取得短信老数据
			List<BSms> oldDatas = mSmsMapper.getSmsByPhone(phone);
			// 如果老数据存在，并且距离上次请求时间不足1分钟
			if (null != oldDatas) {
				// 如果当日请求超过10条
				if (oldDatas.size() > 10 && oldDatas.get(9).getCreateTime() >= TimeUtil.getCurrentDayZero()) {
					ret.setMsg("今日请求数量大于10次");
					ret.setCode(ResultMsg.ERROR);
					return ret;
				}

				// 如果距离上次请求时间不足1分钟
				if (oldDatas.size() > 1 && TimeUtil.currentTimeMillis() - oldDatas.get(0).getCreateTime() < V_TIME) {
					ret.setMsg("请求过于频繁，请稍后再试");
					ret.setCode(ResultMsg.ERROR);
					return ret;
				}
			}
			// 随机生成一个验证码
			String code = UUIDUtils.getSmsCode();

			BSms newSms = new BSms();
			newSms.setCode(code);
			newSms.setPhone(phone);
			newSms.setFlg(0);
			long  nowtime =TimeUtil.currentTimeMillis();
			newSms.setCreateTime(nowtime);
			newSms.setAddTime(nowtime);
			newSms.setAddUser(String.valueOf(user.getUserId()));
			newSms.setUpdateTime(nowtime);
			newSms.setUpdateUser(String.valueOf(user.getUserId()));


			SMSParm mSMSParm = new SMSParm();
			mSMSParm.TemplateID = SMSConfig.SMS; // 模板ID
			String[] a = { "+86" + phone }; // 发送对象电话号码
			mSMSParm.PhoneNumberSet = a;
			String[] parm = { code, "5" }; // 模板参数
			mSMSParm.TemplateParamSet = parm;


			SendSmsResponse smsRet = SMSTool.sendSMS(JSON.toJSONString(mSMSParm));
			if (SMSTool.isSendSMSOK(smsRet)) {
				// 发送成功
				ret.setMsg("请求成功！");
				ret.setCode(ResultMsg.SUCESS);
				// 插入數據庫
				int mresourt = mSmsMapper.createSms(newSms);
				//System.out.println(mresourt);
			} else {
				// 发送失败
				ret.setMsg("连接异常！");
				ret.setCode(ResultMsg.ERROR);
			}


		}catch (Exception e) {
			ret.setCode(ResultMsg.ERROR);
			ret.setMsg("未知的错误，请稍候再试！");
			e.printStackTrace();
		}
		return ret;
	}


}